package com.ruoyi.web.controller.myController;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.ruoyi.common.config.CosConfig;
import com.ruoyi.common.config.lphCosConfig;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.service.*;
import com.ruoyi.system.service.impl.SelectUser;
import com.ruoyi.web.controller.common.R;
import com.ruoyi.web.controller.utils.RedisUtil;
import com.ruoyi.web.controller.utils.SendMail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@RestController
@RequestMapping("/api")
public class    UpdateUserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SendMail sendMail;
    @Autowired
    private IMyuserService userMapper;
    @Autowired
    private IMyuserService iMyuserService;
    @Autowired
    private IUserinfoService iUserinfoService;
    @Autowired
    private IUserskillService iUserskillService;
    @Autowired
    private IWebService webMapper;
    @Autowired
    private IJwebService jwebMapper;
    @PostMapping("/updateUserInfo")
    public Results updateUserInfo(@RequestBody UpdateUser updateUser){
        Myuser myuser = userMapper.selectMyuserByUserid(updateUser.getUserid().longValue());

        if(myuser.getEmail().equals(updateUser.getEmail())) {
            Myuser myuser1 = iMyuserService.selectMyuserByUserid(updateUser.getUserid().longValue());
            myuser1.setUserName(updateUser.getUsername());
            iMyuserService.updateMyuser(myuser1);
            Userinfo info = iUserinfoService.selectUserinfoByUid(updateUser.getUserid().longValue());
            info.setSex(updateUser.getSex().longValue());
            info.setSkill(updateUser.getSkill().longValue());
            User users = new User(myuser1.getUserid(),myuser1.getImage(),myuser1.getUserName(),myuser1.getLoginName(),myuser1.getPassword(),myuser1.getEmail(),updateUser.getSkill(),myuser1.getNdate(),myuser1.getNdate());
            userService.updateUser(users);
            iUserinfoService.updateUserinfo(info);
            iUserskillService.deleteUserskillByUserid(updateUser.getUserid().longValue());
            Userskill userskill = null;
            for (SkillTable skillTable : updateUser.getSkillTable()) {
                userskill = new Userskill(updateUser.getUserid().longValue(), info.getSkill(), skillTable.getId().longValue(), skillTable.getInfoid().longValue(), skillTable.getName());
                iUserskillService.insertUserskill(userskill);
            }
            return new Results(200, "修改昵称成功", myuser1);
        }
        else{
            Integer o = Integer.parseInt(redisUtil.get(updateUser.getLoginname()).toString());
            if(o.equals(updateUser.getCode())) {
                Myuser myuser1 = iMyuserService.selectMyuserByUserid(updateUser.getUserid().longValue());
                myuser1.setUserName(updateUser.getUsername());
                myuser1.setEmail(updateUser.getEmail());
                iMyuserService.updateMyuser(myuser1);
                Userinfo info = iUserinfoService.selectUserinfoByUid(updateUser.getUserid().longValue());
                info.setSex(updateUser.getSex().longValue());
                info.setSkill(updateUser.getSkill().longValue());
                User users = new User(myuser1.getUserid(),myuser1.getImage(),myuser1.getUserName(),myuser1.getLoginName(),myuser1.getPassword(),myuser1.getEmail(),info.getSkill().intValue(),myuser1.getNdate(),myuser1.getNdate());
                userService.updateUser(users);
                iUserinfoService.updateUserinfo(info);
                iUserskillService.deleteUserskillByUserid(updateUser.getUserid().longValue());
                Userskill userskill =null;
                for (SkillTable skillTable : updateUser.getSkillTable()) {
                    userskill = new Userskill(updateUser.getUserid().longValue(), info.getSkill(), skillTable.getId().longValue(), skillTable.getInfoid().longValue(), skillTable.getName());
                    iUserskillService.insertUserskill(userskill);
                }
                return new Results(200, "修改昵称成功", myuser1);
            }else{
                return new Results(500,"验证码是过期了","");
            }
        }
    }
    @PostMapping("/selectUserInfo/{id}")
    public Results selectUserInfo(@PathVariable("id") Integer id){
        Userinfo userinfo = iUserinfoService.selectUserinfoByUid(id.longValue());
        return new Results(200,"",userinfo);
    }
    @PostMapping("/update/SelectSkill")
    public table updateUserSex(@RequestBody Userinfo skill){
        Long skill1 = skill.getSkill();
        List<Web> webs = null;
        List<Jweb> jwebs = null;
        List<SkillTable> list = new ArrayList<SkillTable>();
        if (skill1 == 1l) {
            webs = webMapper.selectWebList(1l);
            for(Web web:webs){
                SkillTable skillTable = new SkillTable(Integer.parseInt(web.getWid().toString()),Integer.parseInt(web.getInfoid().toString()),"前端",web.getName());
                list.add(skillTable);
            }
        }
        if (skill1 == 2l) {
            jwebs = jwebMapper.selectJwebList(skill1);
            for(Jweb jweb:jwebs){
                SkillTable skillTable = new SkillTable(Integer.parseInt(jweb.getJid().toString()),Integer.parseInt(jweb.getInfoid().toString()),"后端",jweb.getName());
                list.add(skillTable);
            }
        }
        if (skill1 == 3l) {
            webs = webMapper.selectWebList(1l);
            for(Web web:webs){
                SkillTable skillTable = new SkillTable(Integer.parseInt(web.getWid().toString()),Integer.parseInt(web.getInfoid().toString()),"前端",web.getName());
                list.add(skillTable);
            }
            jwebs = jwebMapper.selectJwebList(2l);
            for(Jweb jweb:jwebs){
                SkillTable skillTable = new SkillTable(Integer.parseInt(jweb.getJid().toString()),Integer.parseInt(jweb.getInfoid().toString()),"后端",jweb.getName());
                list.add(skillTable);
            }
        }
        return new table(Integer.parseInt(skill.getUid().toString()),list);
    }
    @PostMapping("/UpdategetCode")
    public Results getCode(@RequestBody LoginDTO loginDTO){
        String loginName = loginDTO.getLoginName();
            String email = loginDTO.getEmail();
            Myuser myuser = userMapper.selectMyuserByEmail(email);
            if(myuser!=null){
                return new Results(111,"该邮箱已经注册过了！","");
            }
            String s = "";
            Random random = new Random();
            for (int i = 0; i < 6; i++) {
                s = s + String.valueOf(random.nextInt(10));
            }
            sendMail.send(s,email);
            redisUtil.set(loginName,s,120);
            return new Results(200,"获取验证码成功",s);
    }
    @PostMapping("/update/image")
    public Results add(@RequestParam("userid") Integer userid,@RequestParam("file") MultipartFile image )
    {
        String originalFilename = image.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        if( suffix.equals(".jpg")|| suffix.equals(".png") ||suffix.equals(".jpeg") ){
            COSClient cosClient = new lphCosConfig().cosClient();
            String url=null;
            String newfilename= UUID.randomUUID().toString()+suffix;
            ObjectMetadata objectMetadata = new ObjectMetadata();
            try {
                InputStream inputStream = image.getInputStream();
                objectMetadata.setContentLength(inputStream.available());
                PutObjectRequest putObjectRequest1 = new PutObjectRequest(new lphCosConfig().getbucket(),
                        newfilename, inputStream, objectMetadata);
                cosClient.putObject(putObjectRequest1);
                url = "https://" + new lphCosConfig().getbucket() +
                        ".cos.ap-guangzhou.myqcloud.com" + "/" +newfilename;
                Myuser myuser = iMyuserService.selectMyuserByUserid(userid.longValue());
                String key=(myuser.getImage()).substring((myuser.getImage()).lastIndexOf("/"));
                cosClient.deleteObject(new lphCosConfig().getbucket(),key);
                myuser.setImage(url);
                iMyuserService.updateMyuser(myuser);
                User userByName = userService.getUserByName(myuser.getLoginName());
                userByName.setAvatar(myuser.getImage());
                userService.updateUser(userByName);
                return new Results(400,"",url);
            } catch (Exception e) {e.printStackTrace();}finally {cosClient.shutdown();}}
            return new Results(403,"上传失败","");
    }
}
